package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysUser;
import com.farm.kaiping.po.SysUserPO;
import com.farm.kaiping.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户表(SysUser)表控制层
 *
 * @author zhuhui
 * @since 2024-11-30 20:32:48
 */
@RestController
@RequestMapping("/sysUser")
@AllArgsConstructor
@Api(tags = "用户表控制器")
public class SysUserController {

	private final SysUserService sysUserService;

	@ApiOperation(value = "分页查询用户表", notes = "权限：view_sysUser_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_page')")
	@PostMapping("/page")
	public R<Page<SysUser>> getSysUserPage(@RequestBody SysUserPO sysUserPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysUserPO);
		return R.ok(sysUserService.getSysUserPage(page, sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询用户表列表", notes = "权限：view_sysUser_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_list')")
	@PostMapping("/list")
	public R<List<SysUser>> getSysUserList(@RequestBody SysUserPO sysUserPO) {
		try{
			return R.ok(sysUserService.getSysUserList(sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询用户表", notes = "权限 view_sysUser_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysUserService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条用户表", notes = "权限：view_sysUser_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_one')")
	@PostMapping("/one")
	public R<SysUser> getSysUser(@RequestBody SysUserPO sysUserPO) {
		try{
			return R.ok(sysUserService.getSysUser(sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增用户表", notes = "权限：view_sysUser_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_add')")
	@PostMapping("/add")
	public R saveSysUser(@RequestBody SysUserPO sysUserPO) {
		try{
		    return R.ok(sysUserService.saveSysUser(sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改用户表", notes = "权限：view_sysUser_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_edit')")
	@PostMapping("/edit")
	public R updateSysUser(@RequestBody SysUserPO sysUserPO) {
		try{
		    return R.ok(sysUserService.updateSysUser(sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除用户表", notes = "权限：view_sysUser_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_del')")
	@PostMapping("/del")
	public R removeSysUser(@RequestBody SysUserPO sysUserPO) {
		try{
		    return R.ok(sysUserService.removeSysUser(sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	@ApiOperation(value = "用户修改密码表", notes = "权限：view_sysUser_chgPasswd")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysUser_chgPasswd')")
	@PostMapping("/chgPasswd")
	public R changePassword(@RequestBody SysUserPO sysUserPO) {
		try{
			return R.ok(sysUserService.changePassword(sysUserPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
}

